# Environment

ALLIANCE_TOP	= /opt/alliance-5.0

ENV_ASIMUT	= MBK_CATAL_NAME=CATAL_ASIMUT

ENV_COUGAR	= MBK_SPI_MODEL=$(ALLIANCE_TOP)/etc/spimodel.cfg ;\
		  export MBK_SPI_MODEL;\
		  MBK_IN_PH=ap ;\
		  export MBK_IN_PH;\
		  MBK_OUT_LO=spi;\
		  export MBK_OUT_LO

# All

all: translate check-behavioral synthesis place-route transistors

# Translate

translate: ffd.vbe tri_state.vbe and_gate.vbe decoder_6.vbe cell.vst  \
element.vst mux.vbe mux_64.vbe decoder_3.vbe vector_element.vst data_memory.vst

ffd.vbe: ffd.vhdl
	vasy -Vaop -I vhdl ffd

tri_state.vbe: tri_state.vhdl
	vasy -Vaop -I vhdl tri_state

and_gate.vbe: and_gate.vhdl
	vasy -Vaop -I vhdl and_gate

decoder_3.vbe: decoder_3.vhdl
	vasy -Vaop -I vhdl decoder_3

decoder_6.vbe: decoder_6.vhdl
	vasy -Vaop -I vhdl decoder_6

mux.vbe: mux.vhdl
	vasy -Vaop -I vhdl mux

mux_64.vbe: mux_64.vhdl
	vasy -Vaop -I vhdl mux_64

cell.vst: cell.vhdl
	vasy -Vaop -I vhdl cell

element.vst: element.vhdl
	vasy -Vaop -I vhdl element

vector_element.vst: vector_element.vhdl 
	vasy -Vaop -I vhdl -H vector_element

data_memory.vst data_memory_model.vbe: data_memory.vhdl
	vasy -Vaop -I vhdl data_memory

# Simulate

check-behavioral: translate test_data_memory.pat result_sim.pat

test_data_memory.pat: test_data_memory.c
	genpat test_data_memory

result_sim.pat: data_memory.vst test_data_memory.pat
	$(ENV_ASIMUT) asimut data_memory test_data_memory result_sim

view-behav: result_sim.pat
	xpat -l result_sim 

# Synthesize

synthesis: ffd.vst tri_state.vst and_gate.vst decoder_3.vst decoder_6.vst \
mux.vst mux_64.vst cell.vst element.vst vector_element.vst data_memory.vst \
data_memory_model.vst

ffd_boom.vbe: ffd.vbe
	boom -i 10 -d 0 ffd ffd_boom

ffd.vst: ffd_boom.vbe
	boog -m 0 ffd_boom ffd
	loon -m 0 -o ffd

tri_state_boom.vbe: tri_state.vbe
	boom -i 10 -d 0 tri_state tri_state_boom

tri_state.vst: tri_state_boom.vbe
	boog -m 0 tri_state_boom tri_state
	loon -m 0 -o tri_state

and_gate_boom.vbe: and_gate.vbe
	boom -i 10 -d 0 and_gate and_gate_boom

and_gate.vst: and_gate_boom.vbe
	boog -m 0 and_gate_boom and_gate
	loon -m 0 -o and_gate

decoder_3_boom.vbe: decoder_3.vbe
	boom -i 10 -d 0 decoder_3 decoder_3_boom

decoder_3.vst: decoder_3_boom.vbe
	boog -m 0 decoder_3_boom decoder_3
	loon -m 0 -o decoder_3

decoder_6_boom.vbe: decoder_6.vbe
	boom -i 10 -d 0 decoder_6 decoder_6_boom

decoder_6.vst: decoder_6_boom.vbe
	boog -m 0 decoder_6_boom decoder_6
	loon -m 0 -o decoder_6

mux_64_boom.vbe: mux_64.vbe
	boom -i 10 -d 0 mux_64 mux_64_boom

mux_64.vst: mux_64_boom.vbe
	boog -m 0 mux_64_boom mux_64
	loon -m 0 -o mux_64

mux_boom.vbe: mux.vbe
	boom -i 10 -d 0 mux mux_boom

mux.vst: mux_boom.vbe
	boog -m 0 mux_boom mux
	loon -m 0 -o mux

data_memory_model.vst: data_memory_model_boom.vbe
	boog -m 0 data_memory_model_boom data_memory_model
	loon -m 0 -o data_memory_model

data_memory_model_boom.vbe: data_memory_model.vbe
	boom -i 10 -d 0 data_memory_model data_memory_model_boom

view-gates: ffd.vst tri_state.vst and_gate.vst decoder_3.vst decoder_6.vst \
mux.vst mux_64.vst cell.vst element.vst vector_element.vst data_memory.vst
	xsch -l data_memory

# Place and Route

place-route: data_memory_ocp.ap data_memory_nero.ap data_memory_s2r.cif

data_memory_ocp.ap: data_memory.vst
	ocp data_memory data_memory_ocp

data_memory_nero.ap: data_memory_ocp.ap data_memory.vst
	nero -p data_memory_ocp data_memory data_memory_nero

data_memory_s2r.cif: data_memory_nero.ap
	s2r data_memory_nero data_memory_s2r

view-place: data_memory_ocp.ap
	graal -l data_memory_ocp

view-route: data_memory_nero.ap
	graal -l data_memory_nero

view-real: data_memory_s2r.cif
	dreal -l data_memory_s2r

# Extract transistor netlist

transistors: data_memory_transistors.spi vector_element_transistors.spi \
element_transistors.spi cell_transistors.spi ffd_transistors.spi\
tri_state_transistors.spi and_gate_transistors.spi decoder_6_transistors.spi \
decoder_3_transistors.spi mux_transistors.spi mux_64_transistors.spi

data_memory_transistors.spi: data_memory_nero.ap
	$(ENV_COUGAR); cougar -t data_memory_nero data_memory_transistors

vector_element_ocp.ap: vector_element.vst
	ocp vector_element vector_element_ocp

vector_element_nero.ap: vector_element_ocp.ap vector_element.vst
	nero -p vector_element_ocp vector_element vector_element_nero

vector_element_transistors.spi: vector_element_nero.ap
	$(ENV_COUGAR); cougar -t vector_element_nero vector_element_transistors

element_ocp.ap: element.vst
	ocp element element_ocp

element_nero.ap: element_ocp.ap element.vst
	nero -p element_ocp element element_nero

element_transistors.spi: element_nero.ap
	$(ENV_COUGAR); cougar -t element_nero element_transistors

cell_ocp.ap: cell.vst
	ocp cell cell_ocp

cell_nero.ap: cell_ocp.ap cell.vst
	nero -p cell_ocp cell cell_nero

cell_transistors.spi: cell_nero.ap
	$(ENV_COUGAR); cougar -t cell_nero cell_transistors

ffd.ap: ffd.vst
	ocp ffd ffd

ffd_ocp.ap: ffd.vst
	ocp ffd ffd_ocp

ffd_nero.ap: ffd_ocp.ap ffd.vst
	nero -p ffd_ocp ffd ffd_nero

ffd_transistors.spi: ffd_nero.ap
	$(ENV_COUGAR); cougar -t ffd_nero ffd_transistors

tri_state_ocp.ap: tri_state.vst
	ocp tri_state tri_state_ocp

tri_state_nero.ap: tri_state_ocp.ap tri_state.vst
	nero -p tri_state_ocp tri_state tri_state_nero

tri_state_transistors.spi: tri_state_nero.ap
	$(ENV_COUGAR); cougar -t tri_state_nero tri_state_transistors

decoder_3_ocp.ap: decoder_3.vst
	ocp decoder_3 decoder_3_ocp

decoder_3_nero.ap: decoder_3_ocp.ap decoder_3.vst
	nero -p decoder_3_ocp decoder_3 decoder_3_nero

decoder_3_transistors.spi: decoder_3_nero.ap
	$(ENV_COUGAR); cougar -t decoder_3_nero decoder_3_transistors

mux_ocp.ap: mux.vst
	ocp mux mux_ocp

mux_nero.ap: mux_ocp.ap mux.vst
	nero -p mux_ocp mux mux_nero

mux_transistors.spi: mux_nero.ap
	$(ENV_COUGAR); cougar -t mux_nero mux_transistors

decoder_6_ocp.ap: decoder_6.vst
	ocp decoder_6 decoder_6_ocp

decoder_6_nero.ap: decoder_6_ocp.ap decoder_6.vst
	nero -p decoder_6_ocp decoder_6 decoder_6_nero

decoder_6_transistors.spi: decoder_6_nero.ap
	$(ENV_COUGAR); cougar -t decoder_6_nero decoder_6_transistors

mux_64_ocp.ap: mux_64.vst
	ocp mux_64 mux_64_ocp

mux_64_nero.ap: mux_64_ocp.ap mux_64.vst
	nero -p mux_64_ocp mux_64 mux_64_nero

mux_64_transistors.spi: mux_64_nero.ap
	$(ENV_COUGAR); cougar -t mux_64_nero mux_64_transistors

and_gate_ocp.ap: and_gate.vst
	ocp and_gate and_gate_ocp

and_gate_nero.ap: and_gate_ocp.ap and_gate.vst
	nero -p and_gate_ocp and_gate and_gate_nero

and_gate_transistors.spi: and_gate_nero.ap
	$(ENV_COUGAR); cougar -t and_gate_nero and_gate_transistors

spice-data_memory: data_memory_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l data_memory_transistors

spice-vector_element: vector_element_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l vector_element_transistors

spice-element: element_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l element_transistors

spice-cell: cell_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l cell_transistors
	
spice-ffd: ffd_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l ffd_transistors
	
spice-tri_state: tri_state_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l tri_state_transistors

spice-decoder_3: decoder_3_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l decoder_3_transistors

spice-decoder_6: decoder_6_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l decoder_6_transistors

spice-mux: mux_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l mux_transistors

spice-mux_64: mux_64_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l mux_64_transistors

spice-and_gate: and_gate_transistors.spi
	$(ENV_COUGAR); xsch -I spi -l and_gate_transistors

# Clean

clean:
	rm -rf  *.pat \
		*.vst \
		*.xsc \
		*.vbe \
		*.boom \
		*.ap \
		*.cif \
		*.spi \
		*.dat
	clear
